package com.apes.autoparts.parts.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.parts.model.EpcSearchLog;
import com.apes.autoparts.parts.model.EpcSearchLogItem;
import com.apes.autoparts.parts.repository.EpcSearchLogRepository;
import com.apes.autoparts.parts.repository.PartDao;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.generator.service.SequenceService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by lishikun on 2021/5/31 15:34
 */
@Service("epcSearchLogService")
public class EpcSearchLogService {
    public void save(SimpleRequest request) {
        List<EpcSearchLog> epcSearchLogs = new ArrayList<>();
        List<Map> filters = (List<Map>) request.getJO().getJSONObject("request").get("condition");
        String userId = request.getJO().getString("userId");
        JSONObject jo = new JSONObject();
        for (Map filter : filters) {
            if ("dataCenterCondition".equals(filter.get("field"))) jo = JSONObject.parseObject(filter.get("value").toString());
        }
        String CPMC = jo.getString("QCCPMC");
        String CPID = jo.getString("QCCPID");
        String vin = jo.getString("vin");
        Date searchDate = jo.get("searchDate") != null ? jo.getDate("searchDate") : new Date();
        JSONArray searchTexts = request.getJO().getJSONArray("searchTexts");
        JSONArray searchResult = request.getJO().getJSONArray("searchResult");

        for (int i = 0; i < searchTexts.size(); i++) {
            EpcSearchLog epcSearchLog = new EpcSearchLog();
            epcSearchLog.setCreator(userId);
            String CP = ("[" + (CPID != null ? CPID :"") + "]") + CPMC;
            epcSearchLog.setCP(CP);
            epcSearchLog.setCPID(CPID);
            epcSearchLog.setCPMC(CPMC);
            epcSearchLog.setSearchText(searchTexts.getJSONObject(i).getString("keywords"));
            epcSearchLog.setSource(i == 0 ? "用户输入": i== 1 ? "标准名称" :"对照名称" );
            epcSearchLog.setVin(vin);
            epcSearchLog.setSearchTime(searchDate);
            if (searchResult.getJSONObject(i).getJSONObject("data") == null) continue;
            for (Object o : searchResult.getJSONObject(i).getJSONObject("data").getJSONArray("items")) {
                EpcSearchLogItem epcSearchLogItem = new EpcSearchLogItem();
                JSONObject item = (JSONObject)o;
                epcSearchLogItem.setImageUrls(item.getString("imageUrls"));
                epcSearchLogItem.setId(SpringManager.getBean(SequenceService.class).getId(EpcSearchLogItem.CONST_TABLE_NAME));
                epcSearchLogItem.setImageSN(item.getString("imageSN"));
                epcSearchLogItem.setMainGroupId(item.getString("mainGroupId"));
                epcSearchLogItem.setMainGroupName(item.getString("mainGroupName"));
                epcSearchLogItem.setPartCode(item.getString("partCode"));
                epcSearchLogItem.setSubGroupId(item.getString("subGroupId"));
                epcSearchLogItem.setSubGroupName(item.getString("subGroupName"));
                epcSearchLogItem.setPartName(item.getString("partName"));
                epcSearchLogItem.setPartId(item.getString("id"));
                epcSearchLogItem.setPrice(item.getBigDecimal("price") != null ? item.getBigDecimal("price").doubleValue() : 0);
                epcSearchLogItem.setCost(item.getBigDecimal("cost") != null ?item.getBigDecimal("cost").doubleValue() : 0);
                List PJ;
                // 配件
                if (item.getJSONArray("parts") == null) {
                    JSONArray queryPJMC = new JSONArray();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("partCode", item.getString("partCode"));
                    jsonObject.put("PZCXID", jo.getString("PZCXID"));
                    queryPJMC.add(jsonObject);
                    PJ = SpringManager.getBean(PartDao.class).queryCategories(queryPJMC).stream().map(jsonPJ -> {
                        JSONObject json = (JSONObject) jsonPJ;
                        JSONObject object = new JSONObject();
                        object.put("PJMCID", json.getString("PJMCID"));
                        object.put("PJMC", json.getString("PJMC"));
                        return object;
                    }).collect(Collectors.toList());
                } else {
                    PJ = item.getJSONArray("parts").toJavaList(Object.class);
                }
                epcSearchLogItem.setPJ(epcSearchLogItem.convertToString(PJ));
                epcSearchLog.getEpcSearchLogItems().add(epcSearchLogItem);
                epcSearchLogItem.setEpcSearchLog(epcSearchLog);
            }
            epcSearchLogs.add(epcSearchLog);
        }
        SpringManager.getBean(EpcSearchLogRepository.class).save(epcSearchLogs);
    }
}
